Skip to main content

std.bytes

Pebble 0.3.1 · all symbols on this page are stable.

Monomorphic helpers over bytes, exposing every byte-string operation as a first-class function. Use these for partial application or as arguments to higher-order code; prefer operator forms (==, <, +) and methods (b.length()) in regular code.

Methods

FunctionDescription
length(b: bytes): intNumber of bytes.
slice(start: int, len: int, b: bytes): byteslen-byte sub-string starting at start. Clips on out-of-range.
prepend(byteVal: int, b: bytes): bytesPrepend a single byte (0–255). Fails if byteVal is out of range.
concat(a: bytes, b: bytes): bytesConcatenate two byte strings.
indexAt(b: bytes, i: int): intByte at index i as an int 0–255. Fails on out-of-range.
equals(a: bytes, b: bytes): boolEquality.
lessThan(a: bytes, b: bytes): boolLexicographic less-than.
lessThanEquals(a: bytes, b: bytes): boolLexicographic less-than-or-equal.
greaterThan(a: bytes, b: bytes): boolLexicographic greater-than.
greaterThanEquals(a: bytes, b: bytes): boolLexicographic greater-than-or-equal.
toInt(b: bytes): intDecode big-endian unsigned integer.
fromInt(n: int): bytesEncode n as the shortest big-endian unsigned byte string.

Examples

using {
length, slice, prepend, concat, indexAt,
equals, lessThan, lessThanEquals, greaterThan, greaterThanEquals,
toInt, fromInt
} = std.bytes;

const n: int = length(#deadbeef); // 4
const sub: bytes = slice(1, 2, #deadbeef); // #adbe
const pre: bytes = prepend(0xff, #beef); // #ffbeef
const cat: bytes = concat(#dead, #beef); // #deadbeef
const at: int = indexAt(#deadbeef, 0); // 222 (0xde)
const eq: bool = equals(#ab, #ab); // true
const lt: bool = lessThan(#ab, #cd); // true
const le: bool = lessThanEquals(#ab, #ab); // true
const gt: bool = greaterThan(#cd, #ab); // true
const ge: bool = greaterThanEquals(#ab, #ab); // true
const i: int = toInt(#04d2); // 1234
const enc: bytes = fromInt(1234); // #04d2

See also

  • bytes — method-call form
  • std.builtins — finer-grained byte string and bitwise builtins
  • std.crypto — hash functions consume bytes